package com.his.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.his.entity.Diagnosis;
import com.his.entity.DiagnosisCatalog;
import com.his.entity.ResponseResult;
import com.his.mapper.DiagnosisMapper;
import com.his.service.IDiagnosisService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 诊断类型（疾病）表 服务实现类
 * </p>
 *
 * @author Demoon Liu
 * @since 2022-02-20
 */
@Service
public class DiagnosisServiceImpl extends ServiceImpl<DiagnosisMapper, Diagnosis> implements IDiagnosisService {

    @Resource
    public DiagnosisMapper diagnosisMapper;

    @Override
    public ResponseResult addDiag(Diagnosis diagnosis) {
        int code;
        String message;

        if(diagnosisMapper.insert(diagnosis)>0) {
            message = "插入成功";
            code = 201;
        }
        else {
            message = "插入失败";
            code = 400;
        }
        Map<String, Object> map = new HashMap<String, Object>();
        map.put(" diagnosis", diagnosis);


        return new ResponseResult(code, message,map);
    }

    @Override
    public ResponseResult deleteDiag(long id) {
        int code;
        String message;
        System.out.println("jlll");

        if(diagnosisMapper.deleteById(id)>0) {
            message = "删除成功";
            code = 201;
        }
        else {
            message = "删除失败";
            code = 400;
        }



        return new ResponseResult(code, message,"");
    }

    @Override
    public ResponseResult selectDiag(String str) {
        int code;
        String message;
        QueryWrapper<Diagnosis> wrapper = new QueryWrapper<>();
        // 左和右  t%
        wrapper.like("name", str)
                .or().like("code",str);
        List<Map<String, Object>> map = diagnosisMapper.selectMaps(wrapper);

        System.out.println(str);
        if(map.size()>0) {
            message = "查询成功.";
            code = 201;
        }
        else {
            message = "查询失败";
            code = 400;
        }
        return new ResponseResult(code, message,map);
    }

    @Override
    public ResponseResult updateDiag(Diagnosis diagnosis) {
        int code;
        String message;

        if(diagnosisMapper.updateById(diagnosis)>0) {
            message = "修改成功";
            code = 201;
        }
        else {
            message = "修改失败";
            code = 400;
        }
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("diagnosisCatalog", diagnosis);

        return new ResponseResult(code, message,map);
    }

    @Override
    public ResponseResult selectAll() {
        int code;
        String message;
        List<Map<String, Object>> map = diagnosisMapper.selectMaps(null);
        System.out.println(map);
        if(map.size()>0) {
            message = "查询成功";
            code = 201;
        }
        else {
            message = "查询失败";
            code = 400;
        }
        return new ResponseResult(code, message,map);
    }
}
